package com.decent.controller;

import com.decent.entity.User;
import com.decent.entity.common.Message;
import com.decent.service.OrderService;
import com.decent.service.UserService;
import com.decent.util.ScheduledGetOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Objects;

/**
 * @Author decent
 * @Create 2021/3/31 - 17:13
 * @Description
 */
@Controller
public class UserController {

    @Autowired
    private ScheduledGetOrder scheduledGetOrder;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

//    @RequestMapping("/login")
//    public void login() {
//
//    }

    @RequestMapping(value = "/login")
    public String login(User user, Map<String, Object> map, HttpSession session){
        Message message = userService.login(user);
        if(message.getErrorCode() == 200){

            scheduledGetOrder.getOrder(orderService);
            //登陆成功，防止表单重复提交，可以重定向到主页
            session.setAttribute("loginUser",user.getUname());
            map.put("msg", message);
            return "redirect:/main.html";
        }else{
            //登陆失败
            map.put("msg",message);
            return  "index";
        }

    }

}
